<?php
/**
 * 定义如何处理用户关于题目请求
 *
 * @author judasnow@gmail.com 
 * @time 2010-4-11 2010-5-10
 * @version 1.0
 */
class problem_process {
	/**#@+
         * @access private
	 */

	/**
	 * 保存数据库连接.
	 * 
	 * @var object MDB2
	 */
	private $db ;
	
	/**
	 * 保存用户从页面提交而来的
	 * 信息.
	 * 
	 * @var array
	 */
	private $request ;

	/**
	 * 保存数据库中problem表对应的
	 * 对象.
	 * 
	 * @var object db_obj_problem
	 */
	private $db_obj_problem ;

	/**
	 *
	 * @access public
	 * @param MDB2 $db 
	 * @param array $string 
	 * @return bool
	 */
	function __construct( $db , $request=null ){
		
		$this->db = $db ;		
		$this->db_obj_problem = new db_obj_problem( $db );
		
		if ( !empty( $request ) ){
		
			$this->request = $request ;
			foreach( $request as $k => $v ){

				$this->db_obj_problem->set( $k , $v );
			}
		}

	}

	/**
	 * 委托db_obj_problem中的save()方法
	 *
	 * @access public
	 */
	function save(){

		return  $this->db_obj_problem->save() ;
	}
	/**
	 * 委托db_obj_problem中的get()方法
	 *
	 * @access public
	 * @see db_obj
	 */
	function get( $k ){

		return $this->db_obj_problem->get( $k ) ;
	}

	/**
	 * 委托db_obj_problem中的set()方法
	 * 
	 * @access public
	 * @see db_obj
	 */
	function set( $k , $v ){
       
		return $this->db_obj_problem->set( $k , $v ) ;
	}

	/**
	 * 委托db_obj_problem中的load()方法
	 *
	 * @access public
	 * @see db_obj
	 */
	function load( $k , $v ){
		
		return  $this->db_obj_problem->load( $k , $v ) ;
	}

	/**
	 * 返回所有的题目信息
	 * 
	 * @access public
	 * @param MDB2 $db 
	 * @return array $rows 
	 */
	static function get_all( $db ){

		$sql = "SELECT `problem_no`, `title` , `submit_count` , `ac_count` , `best_user` FROM problem" ;
		$res = $db->query( $sql ); 
		if (PEAR::isError($res)) {
			throw new Exception( '获得题目列表出错'.$res->getMessage() );
		}

		if ( $res->NumRows() >= 1 ){
			
			$rows = $res->fetchAll(MDB2_FETCHMODE_ASSOC) ;
			return $rows ;
		
		}else{
			
			return false ;	
		}
	}
	static function get_by_no( $db,$no ){

		$sql = "SELECT `problem_no`, `title` , `submit_count` , `ac_count` , `best_user` FROM problem where problem_no=".$no ;
		$res = $db->query( $sql ); 
		if (PEAR::isError($res)) {
			throw new Exception( '获得题目列表出错'.$res->getMessage() );
		}
		$rows = $res->fetchAll(MDB2_FETCHMODE_ASSOC) ;
		return $rows;
		
	}
	static function get_by_source( $db,$source ){

		$sql = "SELECT `problem_no`, `title` , `submit_count` , `ac_count` , `best_user` FROM problem where source='".$source."'" ;
		$res = $db->query( $sql ); 
		if (PEAR::isError($res)) {
			throw new Exception( '获得题目列表出错'.$res->getMessage() );
		}
		$rows = $res->fetchAll(MDB2_FETCHMODE_ASSOC) ;
		return $rows;
		
	}

	/**
	 * 增加problem表中的submit_count
	 * 在有用户提交关于本题目的代码
	 * 时,会触发本函数
	 * 
	 * @access public
	 * @return bool 
	 */
	function plus_submit_count(){
		
		$submit_count = $this->db_obj_problem->get( 'submit_count' );
		$this->db_obj_problem->set( 'submit_count' , $submit_count+1 );

		return true ;
	}

	/**
	 * 增加problem表中的ac_count
	 * 在用户提交正确代码后此函数
	 * 会被触发
	 *
	 * @access public
	 * @return bool
	 */
	function plus_ac_count(){
		
		$ac_count = $this->db_obj_problem->get( 'ac_count' );
		$this->db_obj_problem->set( 'ac_count' , $ac_count+1 );

		return true ;
	}
    function delete_by_no($no){
		$sql = "DELETE FROM problem WHERE problem_no =".$no ;
		$res = $this->db->query( $sql );
        		
		if (PEAR::isError($res)) {
			throw new Exception( '删除出错'.$res->getMessage() );
		}
	}
     function get_Max_Problem_no(){
		$sql = "select max(problem_no) FROM problem";
		$res = $this->db->query( $sql ); 
		if (PEAR::isError($res)) {
			throw new Exception( '删除出错'.$res->getMessage() );
		}
		if ( $res->NumRows() == 1 ){
			$rows = $res->fetchRow( MDB2_FETCHMODE_ASSOC );
			$max_no=$rows['max(problem_no)'];
			return $max_no;
		}
		else{
		    return 0;
		}
		
	}
	//
	function update_best_user(){

		$problem_no = $this->db_obj_problem->get('problem_no');

		$sql = "SELECT username FROM result 
			WHERE `problem_no` = $problem_no AND `result` = 'Accepted' 
			ORDER BY time_spend" ;

		$res = $this->db->query( $sql ); 
		if (PEAR::isError($res)) {
			throw new Exception( '更新bset_user出错'.$res->getMessage().$sql );
		}

		$row = $res->fetchRow(MDB2_FETCHMODE_ASSOC) ;
		$this->db_obj_problem->set('best_user' , $row['username'] );

		return true ;
	}
}

